1265B - Beautiful Numbers - CodeForces Solution


data structures implementation math two pointers *1300

Please click on ads to support us..

Python Code:

t = int(input())

for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    pos = [0] * (n + 1)

    for i, e in enumerate(arr):
        pos[e] = i

    l = n
    r = -1

    for i in range(1, n+1):
        l = min(l, pos[i])
        r = max(r, pos[i])

        if r - l + 1 == i:
            print('1', end='')
        else:
            print('0', end='')
    print()




C++ Code:

#include <bits/stdc++.h>
using namespace std;

#define ll long long int
#define Max INT_MAX
#define Min INT_MIN
#define in(a) cin >> a
#define inn(a, b) cin >> a >> b
#define innn(a, b, c) cin >> a >> b >> c
#define fwl(i, a, b) for (int i = a; i < b; i = i + 1)
#define bwl(i, a, b) for (int i = a; i >= b; i = i - 1)
#define invec(a, n) fwl(i, 0, n) cin >> a[i]
#define autol(x, a) for (auto &x : a)
#define all(v) (v.begin(), v.end())
#define vi vector<int>
#define vl vector<long long>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define vs vector<string>
#define pb push_back
#define ppb pop_back
#define pii pair<int, int>
#define vpii vector<pii>
#define makp make_pair
#define unmapii unordered_map<int, int>
#define mapii map<int, int>
#define unsi unordered_set<int>
#define si set<int>
#define ff first
#define ss second

void primeFactors(int n, vi pf)
{
    while (n % 2 == 0)
    {
        pf.pb(2);
        n = n / 2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2)
    {
        while (n % i == 0)
        {
            pf.pb(i);
            n = n / i;
        }
    }
    if (n > 2)
        pf.pb(n);
}

ll fact(int n)
{
    ll p = 1;
    for (ll i = 1; i <= n; i++)
        p *= i;
    return p;
}

ll lcm(ll a, ll b)
{
    return (a * b) / __gcd(a, b);
}

int fastpower(int a, int b, int m)
{
    if (b == 1)
        return a % m;
    if (b % 2 == 0)
    {
        int t = fastpower(a, b / 2, m);
        return (t * t) % m;
    }
    else
    {
        int t = fastpower(a, (b - 1) / 2, m);
        t = (t * t) % m;
        return a * t % m;
    }
}
ll sum(ll n)
{
    return (n * (n + 1)) / 2;
}

void BT()
{
    int n;
    cin >> n;
    vi vec(n);
    invec(vec, n);
    unmapii un;
    fwl(i, 0, n)
    {
        un[vec[i]] = i;
    }
    int l = un[1];
    int r = un[1];
    string ans;
    ans.push_back('1');
    fwl(i, 2, n + 1)
    {
        l = min(l, un[i]);
        r = max(r, un[i]);
        if ((r - l + 1) == i)
        {
            ans.pb('1');
        }
        else
        {
            ans.pb('0');
        }
    }
    cout << ans << endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t = 1;
    cin >> t;
    for (int i = 1; i <= t; i++)
    {
        BT();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

281A - Word Capitalization
1646A - Square Counting
266A - Stones on the Table
61A - Ultra-Fast Mathematician
148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets